import Constants from "../libs/Constants";
import GameData from "../libs/GameData";
import Helper from "../libs/HelperMgr";
import GameManager from "./GameManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class CameraManager extends cc.Component {

    public static instance: CameraManager = null;

    public target: cc.Node = null;
    public isFollowing: boolean = false;


    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        CameraManager.instance = this;
    }

    start() {

    }

    update(dt) {
        if (GameManager.instace.gameIsStart && !GameManager.instace.gameIsOver)
            if (this.target && GameData.instance.gameModel) {
                this.isFollowing = true
                this.node.x = Helper.lerp(this.node.x, this.target.x - cc.winSize.width / 2, 0.5)
                this.node.y = Helper.lerp(this.node.y, this.target.y - cc.winSize.height / 2, 0.5)
            } else {
                this.isFollowing = false;
                this.node.setPosition(0, 0)
            }
    }
}
